<?php

/**
 *  
 */
class AdminBlogArticleCategoryController extends AdminBaseController
{
    public $blog;
    
    /**
     *  before ...
     */
    public function beforeAction($action)
    {
        parent::beforeAction($action);

        $blog = $this->serviceManager->getFocusBlog();
        if( !$blog ) {
            Request::redirect('admin/');
        }

        $this->blog = $blog;
        $this->menus->setFocusByAction('adminBlogArticleCategory/list');
        return true;
    }

    /**
     *  index
     */
    public function actionIndex()
    {
        Request::redirect('list');
    }

    /**
     *  list
     */
    public function actionList( $page=1, $searchKey='' )
    {
        if( Request::isPost() ) {
            $posts = Request::getPostInfo();
            if( $posts['searchKey'] ) {
                $searchKey = $posts['searchKey'];
            }
        }

        $categories = new BlogArticleCategories();
        $myCategories = $categories->getCategories( $searchKey, $page );
        $rowCount = $categories->getNumCategories( $searchKey );

        $this->pageLimit = new PageLimitManager();
        $this->pageLimit->setBaseUrl( $this->createUrl('') );
        $this->pageLimit->setRowCount( $rowCount );
        $this->pageLimit->setPageShowCount( BaseMapper::DEFAULT_ROW_COUNT );
        $this->pageLimit->setPage( $page );
        $this->pageLimit->setSearchKey( $searchKey );

        $this->render('list',array(
            'searchKey' => $searchKey,
            'page' => $page,
            'blogArticleCategories' => $myCategories
        ));
    }

    /**
     *  edit
     */
    public function actionEdit( $id )
    {
        $id = (int) $id;
        $categories = new BlogArticleCategories();
        $category = $categories->getCategory($id);
        if(!$category) {
            Request::redirect('list');
        }

        // update
        if( Request::isPost() ) {
            $posts = Request::getPostInfo();
            $category->setKeyName              ( $posts['keyName']              );
            $category->setName                 ( $posts['name']                 );
            $category->setDescription          ( $posts['description']          );
            $category->setStatus               ( $posts['status']               );
            $category->setOrderedPages         ( $posts['orderedPages']         );
            $category->setNumItems             ( $posts['numItems']             );

            $messages = $category->validate();
            $category->filter();
            if( $messages ) {
                SessionManager::setErrorMessages('更新失敗');
                $data['fieldsMessages'] = $messages;
                $data['blogArticleCategory'] = $category;
                $this->render('edit', $data );
                return;
            }
            $categories->updateCategory($category);
            SessionManager::setSuccessMessages('更新成功');
            Request::redirect('list');  // redirect 之後會清掉訊息
            exit;

        }

        // edit
        $data['blogArticleCategory'] = $category;
        $this->render('edit',$data);
    }

    /**
     *  new
     */
    public function actionNew()
    {

        // add 
        if( Request::isPost() ) {
            $posts = Request::getPostInfo();
            $category = new BlogArticleCategory();
            $category->setKeyName              ( $posts['keyName']              );
            $category->setName                 ( $posts['name']                 );
            $category->setDescription          ( $posts['description']          );
            $category->setStatus               ( $posts['status']               );
            $category->setOrderedPages         ( $posts['orderedPages']         );
            $category->setNumItems             ( $posts['numItems']             );

            $messages = $category->validate();
            $category->filter();
            if( $messages ) {
                SessionManager::setErrorMessages('新增失敗');
                $data['fieldsMessages'] = $messages;
                $data['blogArticleCategory'] = $category;
                $this->render('new', $data );
                return;
            }
            $blogArticleCategories = new BlogArticleCategories();
            $blogArticleCategories->addCategory($category);
            SessionManager::setSuccessMessages('新增成功');

        }

        // new
        $data['blogArticleCategory'] = new BlogArticleCategory();
        $this->render( 'new', $data );

    }

}
